//
//  SwitchViewController.m
//  UESTCPioneer
//
//  Created by sss on 14-9-25.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "SwitchViewController.h"

#pragma mark - 颜色
#define kNavigationBarColor [UIColor colorWithRed:192.0/255.0 green:57.0/255.0 blue:43.0/255.0 alpha:1]
#define KSPEColor [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1]
#define KBtnTextColor [UIColor colorWithRed:119.0/255.0 green:119.0/255.0 blue:119.0/255.0 alpha:1]

#define kOffsetY 47
#define kWidth 320

#define kOffsetH 165

#define kToRight @"toRight"
#define kToLeft @"toLeft"

@interface SwitchViewController (){
    UIView *slideBar;
    int displayBtnTag;
}

@end

@implementation SwitchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}


#pragma mark -
#pragma mark 初始化
- (void)initWithNameArr:(NSArray *)nameArr{
    if (self) {
        self.btnTitleNameArr = nameArr;
        //isView1Display = YES  ------>View1显示View2隐藏
        //isView1Display = NO   ------>View2显示View1隐藏
        self.isView1Display = YES;
        displayBtnTag = 1;
        [self setHeadViewWithNum:nameArr.count];
        [self.view addSubview:self.headView];
        [self setButton];
        [self.view addSubview:self.view1];
        [self.view addSubview:self.view2];
    }
}

//1.set head view
-(void)setHeadViewWithNum:(int)num{
    if (!_headView) {
        _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 47)];
        _headView.backgroundColor = [UIColor whiteColor];
        slideBar = [[UIView alloc] initWithFrame:CGRectMake(0, 43, kWidth/num, 4)];
        slideBar.backgroundColor = kNavigationBarColor;
        [_headView addSubview:slideBar];
        
    }
}
//2.set button
-(void)setButton{
    for (int i = 0; i < self.btnTitleNameArr.count; i++) {
        UIColor *titleColor = (i == 0) ? kNavigationBarColor : KBtnTextColor;
        UIButton *btn = [self addBtn:CGRectMake(i*kWidth/self.btnTitleNameArr.count, 0, kWidth/self.btnTitleNameArr.count, 47) title:self.btnTitleNameArr[i] tag:(i + 1) normalColor:titleColor];
        [self.headView addSubview:btn];
    }
}
-(UIButton *)addBtn:(CGRect)rect title:(NSString*)title tag:(NSInteger)tag normalColor:(UIColor*)color{
    UIButton *btn = [[UIButton alloc] initWithFrame:rect];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.tag = tag;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitleColor:kNavigationBarColor forState:UIControlStateHighlighted];
    [btn setTitleColor:color forState:UIControlStateNormal];
    return btn;
}
-(void)btnClick:(UIButton*)btn{
    if (btn.tag == displayBtnTag) {
        return;
    }
    
    [self btnClickAction:btn];
    
    for (UIView *tempView in self.headView.subviews) {
        if (tempView.tag >0) {
            UIButton *btn1 = (UIButton*)tempView;
            [btn1 setTitleColor:KBtnTextColor forState:UIControlStateNormal];
        }
    }
    
    [btn setTitleColor:kNavigationBarColor forState:UIControlStateNormal];
    
    [self slideBarAnimation:(btn.tag-1)];
    
    if ((btn.tag - displayBtnTag) > 0) {
        [self slideViewWithDisplayType:self.isView1Display to:kToLeft];
    }else{
        [self slideViewWithDisplayType:self.isView1Display to:kToRight];
    }
    displayBtnTag = btn.tag;
    
    
}
-(void)slideBarAnimation:(int)selelctTag{
    CGRect newFram = slideBar.frame;
    newFram = CGRectMake(selelctTag * kWidth/self.btnTitleNameArr.count, 43, kWidth/self.btnTitleNameArr.count, 4);
    [UIView beginAnimations:@"slide" context:nil];
    [UIView setAnimationDuration:0.40];
    [slideBar setFrame:newFram];
    [UIView commitAnimations];
}
//3.set view1 view2
-(UIView *)view1{
    if (!_view1) {
        _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, kOffsetY + 1, kWidth, self.view.frame.size.height - kOffsetH)];
    }
    return _view1;
}
-(UIView *)view2{
    if (!_view2) {
        _view2 = [[UIView alloc] initWithFrame:CGRectMake(kWidth, kOffsetY + 1, kWidth, self.view.frame.size.height - kOffsetH)];
    }
    return _view2;
}

//根据当前显示的页面方式来决定滑动方式-----View1显示View2隐藏   或者   View2显示View1隐藏 ???
-(void)slideViewWithDisplayType:(BOOL)disType to:(NSString*)toDirection{
    if (disType) {
        [self slideView:self.view1 ToDirection:toDirection isHide:NO];
        [self slideView:self.view2 ToDirection:toDirection isHide:YES];
    }else{
        [self slideView:self.view2 ToDirection:toDirection isHide:NO];
        [self slideView:self.view1 ToDirection:toDirection isHide:YES];
    }
    self.isView1Display = !self.isView1Display;
}
-(void)slideView:(UIView*)view ToDirection:(NSString*)toDirection isHide:(BOOL)isHide{
    CGRect viewFram = view.frame;
    if (isHide) {
        [self initHideViewWithSlideDirection:toDirection forView:view];
        viewFram = CGRectMake(0, kOffsetY, kWidth, view.frame.size.height);
    }else{
        if ([toDirection isEqualToString:kToRight]) {
            viewFram = CGRectMake(kWidth, kOffsetY, kWidth, view.frame.size.height);
        }
        if ([toDirection isEqualToString:kToLeft]) {
            viewFram = CGRectMake(-kWidth, kOffsetY, kWidth, view.frame.size.height);
        }
    }
    [UIView beginAnimations:@"viewSlide" context:nil];
    [UIView setAnimationDuration:0.40];
    [view setFrame:viewFram];
    [UIView commitAnimations];
}
-(void)initHideViewWithSlideDirection:(NSString*)direction forView:(UIView*)view{
    CGRect hideFram = view.frame;
    if ([direction isEqualToString:kToRight]) {
        hideFram = CGRectMake(-320, kOffsetY, kWidth, view.frame.size.height);
    }else if([direction isEqualToString:kToLeft]){
        hideFram = CGRectMake(320, kOffsetY, kWidth, view.frame.size.height);
    }
    view.frame = hideFram;
}
#pragma mark -
#pragma mark load
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initWithNameArr:self.btnTitleNameArr];
    UIView *barLine = [[UIView alloc] initWithFrame:CGRectMake(0, 47, 320,1)];
    barLine.backgroundColor = KSPEColor;
    [self.view addSubview:barLine];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark -
#pragma mark 子类调用的函数
-(void)btnClickAction:(UIButton *)btn{
    NSLog(@"父类在调用，请重写btnClickAction:(UIButton *)btn方法");
    
}

@end
